/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.uml_generator.manager.impl;

import java.util.HashSet;
import java.util.Set;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.uml_generator.Constants;
import com.genia.toolbox.uml_generator.manager.AttributeManager;
import com.genia.toolbox.uml_generator.model.AbstractClassModel;
import com.genia.toolbox.uml_generator.model.AttributeModel;
import com.genia.toolbox.uml_generator.model.OperationModel;
import com.genia.toolbox.uml_generator.model.impl.AttributeModelImpl;
import com.genia.toolbox.uml_generator.model.impl.OperationModelImpl;

/**
 * Implementation of {@link AttributeManager}.
 */
public class AttributeManagerImpl
    extends AbstractManager
    implements AttributeManager
{

  /**
   * returns an {@link OperationModel} corresponding to the getter of an
   * attribute.
   * 
   * @param attribute
   *          the {@link AttributeModel} to get the getter for
   * @return an {@link OperationModel} corresponding to the getter of the
   *         attribute
   * @see com.genia.toolbox.uml_generator.manager.AttributeManager#getGetter(com.genia.toolbox.uml_generator.model.AttributeModel)
   */
  public OperationModel getGetter(AttributeModel attribute)
  {
    OperationModel operation = new OperationModelImpl();

    operation.setName("get" + getNameManager().getUpperCaseFirstLetterName(attribute));
    operation.setComment("getter for the " + attribute.getName() + " property.");
    AttributeModel returnType = new AttributeModelImpl();
    returnType.setName("return");
    returnType.setComment("the " + attribute.getName() + " property.");
    returnType.setType(attribute.getType());
    operation.setReturnType(returnType);
    operation.setBody("return " + attribute.getName() + ";");

    return operation;
  }



  /**
   * return a {@link ParameterModel} for a <code>void</code> return type.
   * 
   * @return a {@link ParameterModel} for a <code>void</code> return type
   */
  public AttributeModel getVoidReturnType()
  {
    AttributeModel returnType = new AttributeModelImpl();
    returnType.setName("return");
    returnType.setType(Constants.VOID_MODEL);
    return returnType;
  }



  /**
   * returns an {@link OperationModel} corresponding to the setter of an
   * attribute.
   * 
   * @param attribute
   *          the {@link AttributeModel} to get the setter for
   * @return an {@link OperationModel} corresponding to the setter of the
   *         attribute
   * @see com.genia.toolbox.uml_generator.manager.AttributeManager#getSetter(com.genia.toolbox.uml_generator.model.AttributeModel)
   */
  public OperationModel getSetter(AttributeModel attribute)
  {
    OperationModel operation = new OperationModelImpl();

    operation.setName("set" + getNameManager().getUpperCaseFirstLetterName(attribute));
    operation.setComment("setter for the " + attribute.getName() + " property.");

    operation.setReturnType(getVoidReturnType());

    AttributeModel parameter = new AttributeModelImpl();
    parameter.setName(attribute.getName());
    parameter.setComment("the " + attribute.getName() + " property.");
    parameter.setType(attribute.getType());
    operation.addParameterModels(parameter);

    operation.setBody("this." + attribute.getName() + " = " + attribute.getName() + ";");

    return operation;
  }



  /**
   * return the {@link Set} of {@link AttributeModel} that are new to the
   * {@link AbstractClassModel} given in parameter.
   * 
   * @param classModel
   *          the {@link AbstractClassModel} to inspect.
   * @return the {@link Set} of {@link AttributeModel} that are new to the
   *         {@link AbstractClassModel} given in parameter *
   * @see com.genia.toolbox.uml_generator.manager.AttributeManager#newAttributes(com.genia.toolbox.uml_generator.model.AbstractClassModel)
   */
  public Set<AttributeModel> newAttributes(AbstractClassModel classModel)
  {
    Set<AttributeModel> res = new HashSet<AttributeModel>(classModel.getAttributes());
    if (classModel.getParent() != null) {
      res.removeAll(classModel.getParent().getAttributes());
    }
    return res;
  }



  /**
   * return the {@link Set} of {@link AttributeModel} that are new to the
   * {@link AbstractClassModel} given in parameter.
   * 
   * @param classModel
   *          the {@link AbstractClassModel} to inspect.
   * @return the {@link Set} of {@link AttributeModel} that are new to the
   *         {@link AbstractClassModel} given in parameter *
   * @see com.genia.toolbox.uml_generator.manager.AttributeManager#newAttributesForInterface(com.genia.toolbox.uml_generator.model.AbstractClassModel)
   */
  public Set<AttributeModel> newAttributesForInterface(AbstractClassModel classModel)
  {
    Set<AttributeModel> res = newAttributes(classModel);
    for (AbstractClassModel interfaceModel : classModel.getImplementsModel()) {
      res.removeAll(interfaceModel.getAttributes());
    }
    return res;
  }



  /**
   * return the {@link Set} of {@link AttributeModel} without the identifier if
   * it exists..
   * 
   * @param classModel
   *          the {@link AbstractClassModel} to inspect.
   * @return the {@link Set} of {@link AttributeModel} without the identifier if
   *         it exists.
   * @throws BundledException
   *           when an error occurred
   * @see com.genia.toolbox.uml_generator.manager.AttributeManager#removeIdentifier(com.genia.toolbox.uml_generator.model.AbstractClassModel)
   */
  public Set<AttributeModel> removeIdentifier(AbstractClassModel classModel)
      throws BundledException
  {
    return getCollectionManager().filterSet(newAttributes(classModel), Constants.REMOVE_IDENTIFIER_PREDICATE);
  }

}
